const axios = require("axios").default;

class Reporter {

  constructor(globalConfig, reporterOptions, reporterContext) {
    this._globalConfig = globalConfig;
    this._options = reporterOptions;
    this._context = reporterContext;
  }

  onRunComplete(testContexts, results) {
    let url = this._options.url;
    let data = {
      appid: this._options.appid,
      testdata: Buffer.from(JSON.stringify(results), "utf-8").toString("base64"),
      dingNotice: this._options.dingNotice,
      successNotice: this._options.successNotice
    };
    axios.post(url, data).then(response => {
      if (response.data && response.data.code === "00") {
        let { batch, reportUrl } = response.data.data;
        console.log("\n");
        console.log("--------------------测试报告--------------------");
        console.log(`批次号: ${batch}`);
        console.log(`报告地址: ${reportUrl}`);
        console.log("-----------------------------------------------");
        console.log("\n");
      }
      else {
        console.log("\n");
        console.log("--------------------测试报告--------------------");
        console.log(`${response.data.message}`);
        console.log("-----------------------------------------------");
        console.log("\n");
      }
    });
  }

  getLastError() {
    if (this._shouldFail) {
      return new Error('Custom error reported!');
    }
  }
}

module.exports = Reporter;